package edu.ucsd.cse110;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import edu.ucsd.cse110.model.Log;

/**
 * A class that contains convenience static methods for various
 */
public class Utility {
	/**
	 * Returns a string representation of a calendar in a specific format.
	 * 
	 * @return Calendar as a string.
	 */
	public static String calendarToString(Calendar c, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		String strdate = null;

		if (c != null) {
			strdate = sdf.format(c.getTime());
		}

		return strdate;
	}

	/**
	 * Returns a string representation of a date in a specific format.
	 * 
	 * @return Date as a string.
	 */
	public static String dateToString(Date c, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		String strdate = null;

		if (c != null) {
			strdate = sdf.format(c);
		}

		return strdate;
	}

	/**
	 * @return The best rep in the list.
	 */
	public static int getWodBestRep(List<Log> list) {
		int highestRep = 0;
		if (list.size() != 0) {
			int highRep = list.get(0).getRep();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getRep() >= highRep) {
					highestRep = list.get(i).getRep();
				}
			}
		}
		return highestRep;
	}

	/**
	 * @return The best round in the list.
	 */
	public static int getWodBestRound(List<Log> list) {
		int highestRound = 0;
		if (list.size() != 0) {
			int highRound = list.get(0).getRound();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getRound() >= highRound) {
					highestRound = list.get(i).getRound();
				}
			}
		}
		return highestRound;
	}

	/**
	 * @return The best time in the list.
	 */
	public static String getWodBestTime(List<Log> theList) {
		long gBestTime = 0;
		String theTime = "";
		long mins, secs;
		if (theList.size() > 0) {
			long bestTime = theList.get(0).getTime();
			for (Log item : theList) {
				if (item.getTime() <= bestTime) {
					bestTime = item.getTime();
				}
			}
			gBestTime = bestTime;
			// convert the bestTime from all Seconds to Mins and Secs
			mins = gBestTime / 1000 / 60;
			secs = (gBestTime / 1000) % 60;
			theTime = String.format("%d:%02d (m:s)", mins, secs);
		} else {
			theTime = "N/A";
		}
		return theTime;
	}
}
